package com.example.androidtest.HeadSculpture;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

public class RoundHeadSculptureImageView extends AppCompatImageView   {

    public RoundHeadSculptureImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public RoundHeadSculptureImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoundHeadSculptureImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    @Override
    protected void onDraw(Canvas canvas) {

        Drawable drawable = getDrawable();

        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }

        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
        Bitmap b = Bitmap.createBitmap(width, height, config);

        //画布
        Canvas can = new Canvas(b);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(can);

        if (null == b) {
            return;
        }

        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
        int w = getWidth(), h = getHeight();

        Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    }

    private static Bitmap getCroppedBitmap(Bitmap bitmap, int radius) {
        Bitmap sbitmap;
        if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
            sbitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
        else
            sbitmap = bitmap;
        Bitmap output = Bitmap.createBitmap(sbitmap.getWidth(), sbitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xffa19774;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, sbitmap.getWidth(), sbitmap.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        canvas.drawCircle(sbitmap.getWidth() / 2 + 0.7f, sbitmap.getHeight() / 2 + 0.7f, sbitmap.getWidth() / 2 + 0.1f, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(sbitmap, rect, rect, paint);
        return output;
    }
}
